package com.destinomusical.epura;

import java.util.ArrayList;
import java.util.Collection;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.destinomusical.epura.base.BaseActivity;
import com.destinomusical.epura.obj.Cancion;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.smartthinking.destinomusical.R;

public class MenuCompartir extends BaseActivity{
	private Cancion cancion;
    private static final int PICK_FRIENDS_ACTIVITY = 1;
    private UiLifecycleHelper lifecycleHelper;
    boolean pickFriendsWhenSessionOpened;
	
	public static final String PREFS_NAME = "MyPrefsCompartir";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_compartir);
		cancion = getIntent().getParcelableExtra("cancion");
		
		Button bt2 = (Button) findViewById(R.id.button2);
		bt2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i("click", "click");
				onClickPickFriends();
			}
		});
		
        lifecycleHelper = new UiLifecycleHelper(this, new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {
                onSessionStateChanged(session, state, exception);
            }
        });
        lifecycleHelper.onCreate(savedInstanceState);

        ensureOpenSession();
		
	}
	
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	Log.i("on activity result", "on activity result");
        switch (requestCode) {
            case PICK_FRIENDS_ACTIVITY:
                displaySelectedFriends(resultCode);
                break;
            default:
                Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
                break;
        }
    }
    
    private void displaySelectedFriends(int resultCode) {
        String results = "";
        FriendPickerApplication application = (FriendPickerApplication) getApplication();

        Collection<GraphUser> selection = application.getSelectedUsers();
        if (selection != null && selection.size() > 0) {
            ArrayList<String> names = new ArrayList<String>();
            for (GraphUser user : selection) {
                names.add(user.getName());
            }
            results = TextUtils.join(", ", names);
        } else {
            results = "<No friends selected>";
        }
        Log.i("facebook friends", ""+results);

//        resultsTextView.setText(results);
    }
	
	public void onClickCancel(View vista){
		finish();
	}
	
//	public void onClickFacebook(View vista){
//		Log.i("click", "click");
//		
//		
//		onClickPickFriends();
//		
//		
////    	String texto = getString(R.string.msg_sang) + " " + cancion + ", Karaoke Box!";
////    	Intent intent = new Intent(this, MiFacebook.class);
////    	intent.putExtra("FACEBOOK",texto);
////    	startActivityForResult(intent,11);
////		finish();
//	}
	
	public void onClickTwitter(View vista){
		String texto = getString(R.string.msg_sang) + " " + cancion + ", Karaoke Box!";
    	Intent intent = new Intent(this, Twitter.class);
    	intent.putExtra("TWITTER",texto);
    	startActivityForResult(intent, 12);
    	finish();
	}
	
    private void onClickPickFriends() {
        startPickFriendsActivity();
    }

    private void startPickFriendsActivity() {
        if (ensureOpenSession()) {
            FriendPickerApplication application = (FriendPickerApplication) getApplication();
            application.setSelectedUsers(null);

            Intent intent = new Intent(this, PickFriendsActivity.class);
            // Note: The following line is optional, as multi-select behavior is the default for
            // FriendPickerFragment. It is here to demonstrate how parameters could be passed to the
            // friend picker if single-select functionality was desired, or if a different user ID was
            // desired (for instance, to see friends of a friend).
            PickFriendsActivity.populateParameters(intent, null, true, true);
            startActivityForResult(intent, PICK_FRIENDS_ACTIVITY);
        } else {
            pickFriendsWhenSessionOpened = true;
        }
    }
    private boolean ensureOpenSession() {
        if (Session.getActiveSession() == null ||
                !Session.getActiveSession().isOpened()) {
            Session.openActiveSession(this, true, new Session.StatusCallback() {
                @Override
                public void call(Session session, SessionState state, Exception exception) {
                    onSessionStateChanged(session, state, exception);
                }
            });
            return false;
        }
        return true;
    }
    
    private void onSessionStateChanged(Session session, SessionState state, Exception exception) {
        if (pickFriendsWhenSessionOpened && state.isOpened()) {
            pickFriendsWhenSessionOpened = false;

            startPickFriendsActivity();
        }
    }
	
}
